<?php

namespace controller;

include_once APP_PATH . "./controller/Controller.php";
include_once APP_PATH . "./model/Category.php";

class Category extends Controller{
    // 1. 列表
    // 2. 增加
    // 3. 编辑
    // 4. 删除

    public function categoryList ()
    {
       $this->checkLogin();

        //include_once APP_PATH . "/model/category_list.php";

        $categoryModel = new \model\Category();
        $categoryList = $categoryModel->getCategoryList();

        // 变量作用域
        //include_once APP_PATH . "./template/category_list.php";

        // 包含视图文件
        // 渲染模板
        $this->display('category_list', ['categoryList' => $categoryList]);
    }


    public function categoryAdd()
    {
        session_start();
        if(empty($_SESSION['admin_email'])){
            echo "尚未登录，请先登录。<a href='index.php?c=login'>登录页面</a>";
            exit();
        }

        include_once APP_PATH ."/model/category_add.php";
        include_once APP_PATH ."/view/category_add.php";
    }


    public function categoryAddSave()
    {
        /**
         * 分类增加保存功能
         *
         *
         **/
        //获取分类数据
        $categoryName= $_POST['category_name'];
        $categoryDesc= $_POST['category_desc'];

        session_start();
        if(empty($_SESSION['admin_email'])){
            echo "尚未登录，请先登录。<a href='index.php?c=login'>登录页面</a>";
            exit();
        }

        include_once APP_PATH ."/model/category_add_save.php";
        include_once APP_PATH ."/view/category_add_save.php";
    }


    public function categoryEdit()
    {
        $categoryId = $_GET['category_id'];

        session_start();
        if(empty($_SESSION['admin_email'])){
            echo "尚未登录，请先登录。<a href='index.php?c=login'>登录页面</a>";
            exit();
        }

        include_once APP_PATH ."/model/category_edit.php";
        include_once APP_PATH ."/view/category_edit.php";
    }


    public function categoryEditSave()
    {
        /**
         * 编辑功能
         *
         *
         **/
        //获取分类数据
        $categoryId=$_POST['category_id'];
        $categoryName= $_POST['category_name'];
        $categoryDesc= $_POST['category_desc'];

        session_start();
        if(empty($_SESSION['admin_email'])){
            echo "尚未登录，请先登录。<a href='index.php?c=login'>登录页面</a>";
            exit();
        }

        // 2~45
        if (mb_strlen($categoryName) < 2 || mb_strlen($categoryName) > 45) {
            echo '分类名称2~45个字符。';
            echo '<a href="index.php?c=category_list">返回列表页面</a>';
            //    echo '<a href="javascript:void(0)" onclick="history.back();">返回上一页</a>';
            exit();
        }
        // 10~255
        if (mb_strlen($categoryDesc) < 10 || mb_strlen($categoryDesc) > 255) {
            echo '分类描述10~255个字符。';
            echo '<a href="javascript:void(0)" onclick="history.back();">返回上一页</a>';
            exit();
        }

        include_once APP_PATH . "/model/category_edit_save.php";
        include_once APP_PATH . "/view/category_edit_save.php";
    }


    public function categoryDelete()
    {
        /**
         * 删除功能
         *
         *
         **/
        //获取分类数据
        $categoryId=$_GET['category_id'];

        session_start();
        if(empty($_SESSION['admin_email'])){
            echo "尚未登录，请先登录。<a href='index.php?c=login'>登录页面</a>";
            exit();
        }

        include_once APP_PATH ."/model/category_delete.php";
        include_once APP_PATH ."/view/category_delete.php";
    }
}
